package com.vanzo.bean;

import android.content.Context;
import android.util.Log;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by tisen on 2018/1/22.
 */

public class FollowerDaoUtil {
    private static final String TAG = FollowerDaoUtil.class.getSimpleName();
    private DaoManager mManager;

    public FollowerDaoUtil(Context context) {
        this.mManager = DaoManager.getInstance();
        this.mManager.init(context);
    }

    public boolean insertFollower(Follower follower) {
        boolean flag = false;
        flag = mManager.getDaoSession().getFollowerDao().insert(follower) == -1 ? false : true;
        Log.i(TAG, "insert Follower :" + flag + " --> " + follower.toString());
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     *
     * @param Followers
     * @return
     */
    public boolean insertMultFollower(final List<Follower> Followers) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (Follower Follower : Followers) {
                        mManager.getDaoSession().insertOrReplace(Follower);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 修改一条数据
     *
     * @param Follower
     * @return
     */
    public boolean updateFollower(Follower Follower) {
        boolean flag = false;
        try {
            mManager.getDaoSession().update(Follower);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     *
     * @param Follower
     * @return
     */
    public boolean deleteFollower(Follower Follower) {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().delete(Follower);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     *
     * @return
     */
    public boolean deleteAll() {
        boolean flag = false;
        try {
            //按照id删除
            mManager.getDaoSession().deleteAll(Follower.class);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     *
     * @return
     */
    public List<Follower> queryAllFollower() {
        return mManager.getDaoSession().loadAll(Follower.class);
    }

    /**
     * 根据主键id查询记录
     *
     * @param key
     * @return
     */
    public Follower queryFollowerById(long key) {
        return mManager.getDaoSession().load(Follower.class, key);
    }

    /**
     * 使用native sql进行查询操作
     */
    public List<Follower> queryFollowerByNativeSql(String sql, String[] conditions) {
        return mManager.getDaoSession().queryRaw(Follower.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     *
     * @return
     */
    public List<Follower> queryFollowerByQueryBuilder(long id) {
        QueryBuilder<Follower> queryBuilder = mManager.getDaoSession().queryBuilder(Follower.class);
        return queryBuilder.where(FollowerDao.Properties.TerminalId.eq(id)).list();
    }
}
